package com.stroke.graphics;

import com.stroke.tools.animation.AnimationFile;
import com.stroke.tools.animation.Frame;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: Igor Bubelov
 * Date: 3/5/11 8:21 PM
 */
public class Animation {
    private int current;
    private List<Frame> frames = new ArrayList<Frame>();

    private double updateTimeout;
    private long lastUpdate;

    public Animation(AnimationFile file) {
        file.open();
        frames = new ArrayList<Frame>(file.getFramesSorted());
        file.close();
        current = 0;

        updateTimeout = 1000f / 30;
    }

    public Image getImage() {
        if (System.currentTimeMillis() - lastUpdate >= updateTimeout) {
            next();
            lastUpdate = System.currentTimeMillis();
        }

        return frames.get(current).getImage();
    }

    private void next() {
        if (current != frames.size() - 1) {
            current++;
        } else {
            current = 0;
        }
    }
}